home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xrobots / main.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  11KB  |  335 lines

  1. /*
  2.  * main.c  --  xrobots
  3.  *
  4.  * Send comments, bug reports, etc. to brianw@Sun.COM.
  5.  *
  6.  * 
  7.  * Copyright 1989 Brian Warkentine
  8.  * 
  9.  * Permission to use, copy, modify, distribute, and sell this software and its
  10.  * documentation for any purpose is hereby granted without fee, provided that
  11.  * the above copyright notice appear in all copies and that both that
  12.  * copyright notice and this permission notice appear in supporting
  13.  * documentation, and that the author's name not be used in advertising or
  14.  * publicity pertaining to distribution of the software without specific,
  15.  * written prior permission.  The author makes no representations about the
  16.  * suitability of this software for any purpose.  It is provided "as is"
  17.  * without express or implied warranty.
  18.  * 
  19.  * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
  20.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE 
  21.  * AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
  22.  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
  23.  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
  24.  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  * 
  26.  * The author's current employer has no connection with this software, as it
  27.  * was written before being employed at Sun.  The following address is for 
  28.  * contacting the author only and does not imply any responsibility on the 
  29.  * behalf of Sun Microsystems.
  30.  * 
  31.  * Contact the author at:
  32.  *     Brian Warkentine  (brianw@Sun.COM)
  33.  *     Sun Microsystems
  34.  *     2550 Garcia Avenue
  35.  *     Mountain View, Ca 94043-1100
  36.  *
  37.  */
  38.  
  39. #include <X11/X.h>
  40. #include <X11/Intrinsic.h>
  41. #include <X11/StringDefs.h>
  42. #include <X11/cursorfont.h>
  43. #include <X11/Core.h>
  44. #include <X11/Shell.h>
  45.  
  46. #ifdef R3
  47. #include <X11/Label.h>
  48. #include <X11/Command.h>
  49. #include <X11/Box.h>
  50. #else
  51. #include <X11/Xaw/Label.h>
  52. #include <X11/Xaw/Command.h>
  53. #include <X11/Xaw/Box.h>
  54. #endif
  55.  
  56.  
  57. #include "xrobots.h"
  58.  
  59. /*----------------------------------------------------------------------*/
  60.  
  61. Widget top_shell, top_widget, playfield_widget, score_command, sonic_command;
  62.  
  63. Display *display;
  64. Window playfield;
  65.  
  66. GC gc;
  67. GC cleargc;
  68.  
  69. /*----------------------------------------------------------------------*/
  70.  
  71. static Arg arglisttop_shell[] = {
  72.   {  XtNinput,  (XtArgVal)True },
  73. };
  74.  
  75. static Arg arglistplayfield[] = {
  76.   {  XtNheight, (XtArgVal) pos_to_coord(MAXY)  },
  77.   {  XtNwidth,  (XtArgVal) pos_to_coord(MAXX)  },
  78.   {  XtNborderWidth, (XtArgVal) 4 },
  79. };
  80.  
  81. static Arg arglistteleport_command[] = {
  82.   {  XtNlabel, (XtArgVal) "Teleport"  },
  83. };
  84.  
  85. static Arg arglistwait_command[] = {
  86.   {  XtNlabel, (XtArgVal) "Wait"  },
  87. };
  88.  
  89. static Arg arglistsonic_command[] = {
  90.   {  XtNlabel, (XtArgVal) "Sonic Screwdriver"  },
  91. };
  92.  
  93. static Arg arglistquit_command[] = {
  94.   {  XtNlabel, (XtArgVal) "Quit"  }, 
  95. };
  96.  
  97. static Arg arglistnew_game[] = {
  98.   {  XtNlabel, (XtArgVal) "New Game"  }, 
  99. };
  100.  
  101. static Arg arglistscore_command[] = {
  102.   {  XtNlabel, (XtArgVal) "Score:    0"  }, 
  103. };
  104.  
  105. /*----------------------------------------------------------------------*/
  106.  
  107. static char translations_str[] = 
  108.   "<Btn1Down>:    do_nothing()        \n\
  109.    <Btn2Down>:    do_nothing()        \n\
  110.    <Btn3Down>:    do_nothing()        \n\
  111.    <Btn1Up>:    move()            \n\
  112.    :<Key>u:     move(right, up)        \n\
  113.    :<Key>l:     move(right)        \n\
  114.    :<Key>n:     move(right, down)    \n\
  115.    :<Key>y:     move(left, up)        \n\
  116.    :<Key>h:     move(left)        \n\
  117.    :<Key>b:     move(left, down)    \n\
  118.    :<Key>k:     move(up)        \n\
  119.    :<Key>j:     move(down)        \n\
  120.    :<Key>U:     jump(right, up)        \n\
  121.    :<Key>L:     jump(right)        \n\
  122.    :<Key>N:     jump(right, down)    \n\
  123.    :<Key>Y:     jump(left, up)        \n\
  124.    :<Key>H:     jump(left)        \n\
  125.    :<Key>B:     jump(left, down)    \n\
  126.    :<Key>K:     jump(up)        \n\
  127.    :<Key>J:     jump(down)        \n\
  128.    <Key>.:      move(nowhere)        \n\
  129.    <Key>\\ :    move(nowhere)        \n\
  130.    <Btn2Up>:    go_here()         \n\
  131.    <Btn3Up>:    wait()             \n\
  132.    <Key>s:      sonic()            \n\
  133.    <Key>t:      teleport()         \n\
  134.    <Key>w:      wait()            \n\
  135.    <Key>z:      new_game()        \n\
  136.    Ctrl<Key>c:  quit() ";
  137.  
  138. static void quit_game();
  139.  
  140. static XtActionsRec actions[] = {
  141.   {"wait",      (XtActionProc)wait_for_em},
  142.   {"teleport",  (XtActionProc)teleport},
  143.   {"sonic",     (XtActionProc)sonic_action},
  144.   {"move",      (XtActionProc)move_action},
  145.   {"go_here",    (XtActionProc)go_here_action},
  146.   {"jump",    (XtActionProc)jump_action},
  147.   {"quit",      (XtActionProc)quit_game},
  148.   {"new_game",  (XtActionProc)new_game},
  149.   {"do_nothing",(XtActionProc)do_nothing_action},
  150. };
  151.  
  152. Pixel fg, bg;
  153. Boolean spiffy,            /* limited graphics             */
  154.     autoteleport,        /* teleport when sonic used, and no moves */
  155.     autoteleportalways,    /* teleport when no available moves     */
  156.     showjumps,        /* show no intermediate moves         */
  157.     diewaiting;        /* die if you use wait unsafely     */
  158. XtTranslations translations;
  159.  
  160. static XrmOptionDescRec options[] = {
  161.     {"-spiffy",    "spiffy",    XrmoptionSepArg, NULL },
  162.     {"-autoteleport","autoteleport",XrmoptionSepArg, NULL },
  163.     {"-autoteleportalways","autoteleportalways",XrmoptionSepArg, NULL },
  164.     {"-showmovement","showmovement",XrmoptionSepArg, NULL },
  165.     {"-diewaiting","diewaiting",XrmoptionSepArg, NULL },
  166.     {"-scorefile","scorefile",XrmoptionSepArg, NULL },
  167. };
  168.  
  169. static XtResource application_resources[] = {
  170.   {"foreground", "Foreground", XtRPixel, sizeof(Pixel),
  171.                 (Cardinal)&fg, XtRString, (caddr_t) "Black"},
  172.   {"background", "Background", XtRPixel, sizeof(Pixel),
  173.                 (Cardinal)&bg, XtRString, (caddr_t) "White"},
  174.   {"spiffy", "Spiffy", XtRBoolean, sizeof(Boolean),
  175.                 (Cardinal)&spiffy, XtRString, (caddr_t) "True"},
  176.   {"autoteleport", "Autoteleport", XtRBoolean, sizeof(Boolean),
  177.                 (Cardinal)&autoteleport, XtRString, (caddr_t) "False"},
  178.   {"autoteleportalways", "Autoteleportalways", XtRBoolean, sizeof(Boolean),
  179.                 (Cardinal)&autoteleportalways, XtRString, (caddr_t) "False"},
  180.   {"showmovement", "Showmovement", XtRBoolean, sizeof(Boolean),
  181.                 (Cardinal)&showjumps, XtRString, (caddr_t) "True"},
  182.   {"diewaiting", "Diewaiting", XtRBoolean, sizeof(Boolean),
  183.                 (Cardinal)&diewaiting, XtRString, (caddr_t) "False"},
  184.   {"translations","Translations", XtRTranslationTable, sizeof(XtTranslations),
  185.                 (Cardinal)&translations, XtRString, (caddr_t)translations_str},
  186.   {"scorefile","Scorefile", XtRString, sizeof(String),
  187.                 (Cardinal)&score_filename, XtRString, (caddr_t)SCORE_FILE},
  188. };
  189.  
  190. /*----------------------------------------------------------------------*/
  191.  
  192. main(argc, argv)
  193.   unsigned int argc;
  194.   char **argv;
  195. {
  196.   Arg args[1];
  197.   XGCValues gcv;
  198.   Widget teleport_command,
  199.      wait_command,
  200.      quit_command,
  201.      new_game_command;
  202.  
  203.   srandom(getpid());
  204.  
  205.   top_shell = XtInitialize(argv[0], "xrobots", options, XtNumber(options), &argc, argv);
  206.   XtSetValues(top_shell, arglisttop_shell, XtNumber(arglisttop_shell));
  207.  
  208.   XtAddActions(actions,XtNumber(actions));
  209.  
  210.   XtGetApplicationResources(top_shell, 0, application_resources, 
  211.             XtNumber(application_resources), NULL, 0 );
  212.  
  213.   top_widget = XtCreateManagedWidget(
  214.                                     "top_widget",
  215.                                     boxWidgetClass,
  216.                                     top_shell,
  217.                                     0,0);
  218.  
  219.   playfield_widget = XtCreateManagedWidget(
  220.                                     "playfield",
  221.                                     widgetClass,
  222.                                     top_widget,
  223.                                     arglistplayfield,
  224.                                     XtNumber(arglistplayfield));
  225.  
  226.   XtAugmentTranslations(playfield_widget,translations);
  227.  
  228.   teleport_command = XtCreateManagedWidget(
  229.                                     "teleport_button",
  230.                                     commandWidgetClass,
  231.                                     top_widget,
  232.                                     arglistteleport_command,
  233.                                     XtNumber(arglistteleport_command));
  234.  
  235.   XtAddCallback(teleport_command, XtNcallback, (XtCallbackProc)teleport, 0);
  236.  
  237.   wait_command =  XtCreateManagedWidget(
  238.                                     "wait_button",
  239.                                     commandWidgetClass,
  240.                                     top_widget,
  241.                                     arglistwait_command,
  242.                                     XtNumber(arglistwait_command));
  243.  
  244.   XtAddCallback(wait_command, XtNcallback, (XtCallbackProc)wait_for_em, 0);
  245.  
  246.   sonic_command= XtCreateManagedWidget(
  247.                                     "sonic_button",
  248.                                     commandWidgetClass,
  249.                                     top_widget,
  250.                                     arglistsonic_command,
  251.                                     XtNumber(arglistsonic_command));
  252.  
  253.   XtAddCallback(sonic_command, XtNcallback, (XtCallbackProc)sonic_action, 0);
  254.  
  255.   quit_command = XtCreateManagedWidget(
  256.                                     "quit_button",
  257.                                     commandWidgetClass,
  258.                                     top_widget,
  259.                                     arglistquit_command,
  260.                                     XtNumber(arglistquit_command));
  261.  
  262.   XtAddCallback(quit_command,     XtNcallback, (XtCallbackProc)quit_game, 0);
  263.  
  264.   new_game_command = XtCreateManagedWidget(
  265.                                     "new_game_button",
  266.                                     commandWidgetClass,
  267.                                     top_widget,
  268.                                     arglistnew_game,
  269.                                     XtNumber(arglistnew_game));
  270.  
  271.   XtAddCallback(new_game_command, XtNcallback, (XtCallbackProc)new_game, 0);
  272.  
  273.   score_command = XtCreateManagedWidget(
  274.                                     "score_button",
  275.                                     commandWidgetClass,
  276.                                     top_widget,
  277.                                     arglistscore_command,
  278.                                     XtNumber(arglistscore_command));
  279.  
  280.   XtAddCallback(score_command, XtNcallback, (XtCallbackProc)show_scores_callback, 0);
  281.  
  282.  
  283.   create_high_score_popup(top_widget);
  284.  
  285.   XtRealizeWidget(top_shell);
  286.  
  287.  
  288.   display   = XtDisplay(playfield_widget);
  289.   playfield = XtWindow(playfield_widget);
  290.   gcv.foreground = fg;
  291.   gcv.background = bg;
  292.   gcv.function = GXcopy;
  293.   gc = XCreateGC(display, playfield, 
  294.          GCForeground | GCBackground | GCFunction, &gcv);
  295.   gcv.foreground = bg;
  296.   cleargc = XCreateGC(display, playfield,
  297.           GCForeground | GCBackground | GCFunction, &gcv);
  298.  
  299.   XtAddEventHandler(playfield_widget, ExposureMask, 0, redisplay_level, 0);
  300.   XtAddEventHandler(playfield_widget, PointerMotionMask, 0, pointer_moved, 0);
  301.  
  302.   init_pixmaps(top_shell);
  303.  
  304.   new_game();
  305.  
  306.   XtMainLoop();
  307.  
  308. }
  309.  
  310.  
  311. static void
  312. quit_game()
  313. {
  314.   free_pixmaps();
  315.   XtDestroyWidget(top_shell);
  316.   XFreeGC(display,gc);
  317.   XFreeGC(display,cleargc);
  318.  
  319.   exit(0);
  320. }
  321.  
  322.  
  323. void
  324. update_score(score)
  325.   int score;
  326. {
  327.   char text[13];
  328.   (void)sprintf(text,"Score: %4d",score);
  329.   XtSetArg(arglistscore_command[0],XtNlabel,text);
  330.   XtSetValues(score_command,arglistscore_command,1);
  331. }
  332.  
  333.  
  334.  
  335.